import Vue from 'vue';
import Router from 'vue-router';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
import Cookies from 'Cookies';
import Login from '@/views/login/index';
import main from '@/views/main';
import Iframe from '@/views/iframe';
Vue.use(Router);

const routerPush = Router.prototype.push;
Router.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(error => error);
};

const itemA = {
  orgin:
    process.env.NODE_ENV === 'production'
      ? 'https://notes.jindll.com'
      : 'http://localhost:8128',
  pathName: '/itemA/',
};
const itemB = {
  orgin:
    process.env.NODE_ENV === 'production'
      ? 'https://notes.jindll.com'
      : 'http://localhost:8129',
  pathName: '/itemB/',
};
const itemC = {
  orgin:
    process.env.NODE_ENV === 'production'
      ? 'https://notes.jindll.com'
      : 'http://localhost:9004',
  pathName: '/itemC/',
};

export const menuRouter = [
  {
    path: '/',
    component: main,
    redirect: '/hddl1',
    name: 'hddl',
    meta: {
      name: '获得电力',
      title: '获得电力',
      icon: 'fa fa-home',
      auth: true,
    },
    children: [
      {
        path: '/hddl2',
        name: 'Table',
        component: Iframe,
        meta: {
          ...itemA,
          name: '获得电力2',
          title: '获得电力2',
          auth: true,
        },
      },
      {
        path: '/hddl1',
        name: 'Index',
        component: Iframe,
        meta: {
          ...itemA,
          name: '获得电力1',
          title: '获得电力1',
          auth: true,
        },
      },
    ],
  },
  {
    path: '/data',
    component: main,
    name: 'data',
    redirect: '/bgyd1',
    meta: {
      name: '变更用电',
      title: '变更用电',
      icon: 'fa fa-database',
      auth: true,
    },
    children: [
      {
        // path: '/data/table',
        path: '/bgyd1', // 设置和子应用相同path
        name: 'Chart',
        component: Iframe,
        meta: {
          ...itemA,
          name: '变更用电1',
          title: '变更用电1',
          auth: true,
        },
      },
      {
        // path: '/data/chart',
        path: '/bgyd2',
        name: 'Form',
        component: Iframe,
        meta: {
          ...itemA,
          name: '变更用电2',
          title: '变更用电2',
          auth: true,
        },
      },
      {
        path: '/testMenu',
        name: 'testMenu',
        component: { render: h => h('router-view') }, // TODO 构建一个出口
        meta: {
          name: '测试菜单',
          title: '测试菜单',
          icon: 'fa fa-database',
          auth: true,
        },
        children: [
          {
            path: '/test1',
            name: 'test2',
            component: Iframe,
            meta: {
              ...itemA,
              name: '测试菜单1',
              title: '测试菜单1',
              auth: true,
            },
          },
          {
            path: '/test2',
            name: 'test22',
            component: Iframe,
            meta: {
              ...itemA,
              name: '测试菜单2',
              title: '测试菜单2',
              auth: true,
            },
          },
          {
            path: '/test3',
            name: 'test23',
            component: Iframe,
            meta: {
              ...itemA,
              name: '测试菜单3',
              title: '测试菜单3',
              auth: true,
            },
          },
          {
            path: '/test4',
            name: 'test24',
            component: Iframe,
            meta: {
              ...itemA,
              name: '测试菜单4',
              title: '测试菜单4',
              auth: true,
            },
          },
          {
            path: '/test5',
            name: 'test25',
            component: Iframe,
            meta: {
              ...itemA,
              name: '测试菜单4',
              title: '测试菜单4',
              auth: true,
            },
          },
          {
            path: '/test6',
            name: 'test26',
            component: Iframe,
            meta: {
              ...itemA,
              name: '测试菜单4',
              title: '测试菜单4',
              auth: true,
            },
          },
          {
            path: '/test7',
            name: 'test27',
            component: Iframe,
            meta: {
              ...itemA,
              name: '测试菜单4',
              title: '测试菜单4',
              auth: true,
            },
          },
          {
            path: '/test8',
            name: 'test28',
            component: Iframe,
            meta: {
              ...itemA,
              name: '测试菜单4',
              title: '测试菜单4',
              auth: true,
            },
          },
        ],
      },
    ],
  },
  /* {
    path: '/testMenu',
    name: 'testMenu',
    component: main,
    meta: {
      name: '测试菜单',
      title: '测试菜单',
      icon: 'fa fa-database',
      auth: true,
    },
    children: [
      {
        path: '/test1',
        name: 'test1',
        component: Iframe,
        meta: {
          ...itemA,
          name: '测试菜单1',
          title: '测试菜单1',
          icon: 'fa fa-file-text-o',
          auth: true,
        },
      },
      {
        path: '/test2',
        name: 'test2',
        component: Iframe,
        meta: {
          ...itemA,
          name: '测试菜单2',
          title: '测试菜单2',
          icon: 'fa fa-file-text-o',
          auth: true,
        },
      },
  ],
  }, */
  {
    path: '/ykjr',
    component: main,
    // redirect: '/account/index',
    meta: {
      name: '业扩接入',
      title: '业扩接入',
      icon: 'fa fa-user-plus',
      auth: true,
    },
    children: [
      {
        // path: '/account/index',
        path: '/ykjr1',
        name: 'Account', // 用name通知子应用跳转路由
        component: Iframe,
        meta: {
          ...itemB,
          // orgin: 'http://localhost:8129',
          // pathName: '/itemB/',
          name: '业扩接入1',
          title: '业扩接入1',
          auth: true,
        },
      },
      {
        path: '/genesrv/newspublic/newsPubulic',
        name: 'newsPubulic',
        meta: {
          ...itemC,
          name: '电能替代新闻宣传',
          title: '电能替代新闻宣传',
          auth: true,
        },
        component: Iframe,
      },
    ],
  },
];
export const menuRouterDL = [
  {
    path: '/',
    component: main,
    redirect: '/hddl1',
    name: 'hddl',
    meta: {
      name: '电力',
      title: '电力',
      icon: 'fa fa-home',
      auth: true,
    },
    children: [
      {
        path: '/hddl1',
        name: 'Index',
        component: Iframe,
        meta: {
          ...itemA,
          name: '电力1',
          title: '电力1',
          auth: true,
        },
      },
      {
        path: '/hddl2',
        name: 'Table',
        component: Iframe,
        meta: {
          ...itemA,
          name: '电力2',
          title: '电力2',
          auth: true,
        },
      },
    ],
  },
  {
    path: '/data',
    component: main,
    name: 'data',
    redirect: '/bgyd1',
    meta: {
      name: '用电',
      title: '用电',
      icon: 'fa fa-database',
      auth: true,
    },
    children: [
      {
        // path: '/data/table',
        path: '/bgyd1', // 设置和子应用相同path
        name: 'Chart',
        component: Iframe,
        meta: {
          ...itemA,
          name: '用电1',
          title: '用电1',
          auth: true,
        },
      },
      {
        // path: '/data/chart',
        path: '/bgyd2',
        name: 'Form',
        component: Iframe,
        meta: {
          ...itemA,
          name: '变更用电2',
          title: '变更用电2',
          auth: true,
        },
      },
      {
        path: '/testMenu',
        name: 'testMenu',
        component: { render: h => h('router-view') }, // TODO 构建一个出口
        meta: {
          name: '测试菜单',
          title: '测试菜单',
          icon: 'fa fa-database',
          auth: true,
        },
        children: [
          {
            path: '/test1',
            name: 'test2',
            component: Iframe,
            meta: {
              ...itemA,
              name: '测试菜单1',
              title: '测试菜单1',
              auth: true,
            },
          },
          {
            path: '/test2',
            name: 'test22',
            component: Iframe,
            meta: {
              ...itemA,
              name: '测试菜单2',
              title: '测试菜单2',
              auth: true,
            },
          },
          {
            path: '/test3',
            name: 'test23',
            component: Iframe,
            meta: {
              ...itemA,
              name: '测试菜单3',
              title: '测试菜单3',
              auth: true,
            },
          },
          {
            path: '/test4',
            name: 'test24',
            component: Iframe,
            meta: {
              ...itemA,
              name: '测试菜单4',
              title: '测试菜单4',
              auth: true,
            },
          },
          {
            path: '/test5',
            name: 'test25',
            component: Iframe,
            meta: {
              ...itemA,
              name: '测试菜单4',
              title: '测试菜单4',
              auth: true,
            },
          },
          {
            path: '/test6',
            name: 'test26',
            component: Iframe,
            meta: {
              ...itemA,
              name: '测试菜单4',
              title: '测试菜单4',
              auth: true,
            },
          },
          {
            path: '/test7',
            name: 'test27',
            component: Iframe,
            meta: {
              ...itemA,
              name: '测试菜单4',
              title: '测试菜单4',
              auth: true,
            },
          },
          {
            path: '/test8',
            name: 'test28',
            component: Iframe,
            meta: {
              ...itemA,
              name: '测试菜单4',
              title: '测试菜单4',
              auth: true,
            },
          },
        ],
      },
    ],
  },
  /* {
    path: '/testMenu',
    name: 'testMenu',
    component: main,
    meta: {
      name: '测试菜单',
      title: '测试菜单',
      icon: 'fa fa-database',
      auth: true,
    },
    children: [
      {
        path: '/test1',
        name: 'test1',
        component: Iframe,
        meta: {
          ...itemA,
          name: '测试菜单1',
          title: '测试菜单1',
          icon: 'fa fa-file-text-o',
          auth: true,
        },
      },
      {
        path: '/test2',
        name: 'test2',
        component: Iframe,
        meta: {
          ...itemA,
          name: '测试菜单2',
          title: '测试菜单2',
          icon: 'fa fa-file-text-o',
          auth: true,
        },
      },
  ],
  }, */
  {
    path: '/ykjr',
    component: main,
    // redirect: '/account/index',
    meta: {
      name: '业扩接入',
      title: '业扩接入',
      icon: 'fa fa-user-plus',
      auth: true,
    },
    children: [
      {
        // path: '/account/index',
        path: '/ykjr1',
        name: 'Account', // 用name通知子应用跳转路由
        component: Iframe,
        meta: {
          ...itemB,
          // orgin: 'http://localhost:8129',
          // pathName: '/itemB/',
          name: '业扩接入1',
          title: '业扩接入1',
          auth: true,
        },
      },
      {
        path: '/ykjr2',
        name: 'ykjr2',
        component: Iframe,
        meta: {
          ...itemA,
          name: '业扩接入2',
          title: '业扩接入2',
          auth: true,
        },
      },
    ],
  },
];
const router = new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    {
      path: '/login',
      component: Login,
      meta: { title: '登录' },
      redirect: '/',
      children: [
        {
          path: '/',
          name: 'LoginIndex',
          meta: { title: '登录' },
          component: () => import('@/views/login/login.vue'),
        },
      ],
    },
    {
      path: '/account2',
      component: main,
      children: [
        {
          path: '/account2/user',
          name: 'User',
          meta: {
            ...itemB,
            name: '个人中心',
            title: '个人中心',
            auth: true,
          },
          component: Iframe,
        },
      ],
    },
    ...menuRouter,
  ],
});

// 进度条配置项这样写
NProgress.configure({
  showSpinner: true,
});
router.beforeEach((to, from, next) => {
  // NProgress.start();
  if (to.meta.auth && Cookies.get('auth') !== 'true') {
    next({ path: '/login' });
    // NProgress.done();
    return;
  }
  next();
});
router.afterEach(() => {
  // NProgress.done();
});

export default router;
